<?php
$milestone_url_access = array();
if($this->ion_auth->is_admin($this->session->userdata('user_id'))){
	$milestone_url_access[] = $this->session->userdata('user_id');
}
elseif($this->ion_auth->is_client($this->session->userdata('user_id'))){
	$milestone_url_access[] = $this->session->userdata('user_id');
}
elseif($project_manager == $this->session->userdata('user_id')){
	$milestone_url_access[] = $this->session->userdata('user_id');
}

/* getting all the followers */
$follower_id = array();
foreach($followers as $f){
	$follower_id[] = $f->user_id;
}
?>

<h2>Tasks List of <?= $projects['name']?> </h2>
<ul class="nav nav-tabs" style="font-weight: 500;font-size: 15px">
  <li class="active"><a href="<?php echo site_url();?>">List</a></li>  
  <li><a href="<?php echo site_url('user/notification/project_log/'.$projects['id']);?>">Project Log</a></li>
  <li><a href="<?php echo site_url();?>">Notes</a></li>
  <li><a href="<?php echo site_url();?>">WireFrame</a></li>
</ul>
<?php if($this->session->flashdata('msg')) {?>
<div id="alert" class="alert alert-success">
<i class="icon-ok-circle"></i>  <?= $this->session->flashdata('msg') ?>
</div>
<?php }?>
<div class="row-fluid">
<div class="span10">
<?php if(!$this->ion_auth->is_client($this->session->userdata('user_id'))){?>
<h4>Project Manager : <?php echo ucfirst($this->common_model->get_username_id($projects['assigned_to'])); ?> </h4>
<h4>Followers : </h4> 

<?php 
if ($followers){
foreach($followers as $follower){
	$follows[] = $follower->user_id;	
	?>

<div class="label label-info" id="follower<?php echo $follower->id;?>" data-username="<?php echo $follower->username;?>"> <?php echo $follower->first_name;?> 
  <?php if( ($this->ion_auth->is_admin($this->session->userdata('user_id'))) OR ($projects['assigned_to'] == $this->session->userdata('user_id')) ){ ?>
  <i class="icon-remove" style="cursor:pointer" onclick="remove_follower(<?php echo $follower->id;?>,<?php echo $follower->user_id;?>)"></i>
  <?php }?>
  </div>

<?php }}?>

<?php }?>
<br />

<div class="span12" style="display:none;" id="add_follower">
<h4> Add New Follower </h4>
<form action="<?php echo site_url('user/tasks/add_project_follower/'.$projects['id'])?>" method="POST">
<select name="follower[]" class="span12" multiple="multiple" id="followers" class="selectpicker">
	<?php 
	$follows[] = 0;
	foreach($users as $user) {
		// check whether user is not client and not a follower
		if(!in_array($user->id,$follows) AND !$this->ion_auth->is_client($user->id)){ ?>
	<option value="<?php echo $user->id?>"><?php echo $user->username?></option>
	<?php }	}?>
</select>
<input  type="submit" class="btn btn-small btn-warning" value="Add Follower" data-loading-text="Loading..."/>
</form>
</div>

</div>
<div class="pull-right span2">
<i class="icon-plus-sign"></i>  <a href="#default_group" role="button" data-toggle="modal">Create New Task Group</a>
<br />
  <i class="icon-tasks"></i> <a href="javascript:void(0)" id="view_tasks" onclick="$('#create_tasks').show(500);$(this).hide();$('#tasks_close').show();"> Create Tasks</a>
  <a href="javascript:void(0)" id="tasks_close" style="display:none;" onclick="$('#create_tasks').hide(500);$('#view_tasks').show();$(this).hide();"> Close Tasks</a>
<br />
<?php if( $this->ion_auth->is_admin($this->session->userdata('user_id')) OR ($projects['assigned_to'] == $this->session->userdata('user_id'))){?>
<i class="icon-tags"></i> <a href="javascript:void(0)" id="view" onclick="$('#add_follower').show(500);$(this).hide();$('#close').show();">  Add  Followers</a>
<a href="javascript:void(0)" id="close" style="display:none;" onclick="$('#add_follower').hide(500);$('#view').show();$(this).hide();"> Close Followers</a>
<?php }?>
</div>
</div>

<div class="row-fluid" id="create_tasks" style="display:none;">
	<h4><?= $projects['name']?> :: Add Tasks</h4>
	<form action="<?php echo site_url('user/tasks/create_task');?>" method="POST">
	
	<input type="hidden" value="<?= $projects['id']?>" name="project" />
	
<select name="milestone" class="selectpicker">
	<option value="0">Milestone</option>
	<?php foreach($milestones as $milestone){?>
	<option value="<?= $milestone->id; ?>"><?= $milestone->name; ?></option>
	<?php }?>
</select>

<select name="taskgroup" class="selectpicker">
	<option value="0">Taskgroup</option>
	<?php foreach($taskgroups as $taskgroup){ ?>	
	<option value="<?= $taskgroup->id; ?>"><?= $taskgroup->name; ?></option>
	<?php }?>
	<?php foreach($custom_taskgroups as $custom_taskgroup) {?>
	<option value="<?= $custom_taskgroup->id; ?>"><?= $custom_taskgroup->name; ?></option>
	<?php }?> 	
</select>

<input type="text" class="span2" name="start_date" id="dpd1" placeholder="Star Date" />
<input type="text" class="span2" name="end_date" id="dpd2" placeholder="Due Date"/>
<select name="priority" class="selectpicker">
	<option value="low">Low</option>
	<option value="medium">Medium</option>
	<option value="high">High</option>
</select>
<select name="created_by">
<option value="">Select User</option>
<?php foreach($users as $user){?>
<option value="<?php echo $user->user_id?>"><?php echo $user->username;?></option>
<?php }?>
</select>
<input  type="text" name="title" class="span8" placeholder="Title of the task"/>
<textarea class="span11" placeholder="Description Of Task" name="desc"></textarea>

<input  type="submit" value="Create Task" class="btn btn-info"/>
</form>

</div>
<br /><br />
<?php foreach($taskgroups as $taskgroup) {?>
<?php $tasks = $this->task_model->get_tasks_by_taskgroup_and_project($taskgroup->id,$projects['id']);
if((count($tasks))>0):
?>
<!-- Calculate the % completed -->
<h4 class="widget-name"><i class="icon-folder-open"></i> <?= $taskgroup->name?></h4>
<table width="100%" class="table table-condensed">
<thead>
<tr>
<th>Name</th>
<?php if(!$this->ion_auth->is_client()){?>
<th>Assigned to</th>
<?php }?>
<th>Start Date</th>
<th>Due date</th>
<th>% Completed</th>
<th>Time Spent</th>
<th>Milestone</th>
</tr>
</thead>
<?php foreach($tasks as $task){	
	$task_follower_id = array();
	foreach ($this->task_model->get_task_followers($task->id) as $key => $value){
		$task_follower_id[] = $value->user_id;
	}
	if(!$task->is_completed){
	?>
<tr <?php if($task->completed_percentage == 100){ ?>
class="success"
<?php } elseif($task->completed_percentage == 0) {?>
class="warning"
<?php }?>>
<td>
<?php
if($this->session->userdata('user_id') == $task->user_id){
$milestone_url_access[] = $this->session->userdata('user_id');
}elseif(in_array($this->session->userdata('user_id'),$task_follower_id) ){
	$milestone_url_access[] = $this->session->userdata('user_id');
}elseif((in_array($this->session->userdata('user_id'),$follows))){
	$milestone_url_access[] = $this->session->userdata('user_id');
}elseif((in_array($this->session->userdata('user_id'),$follower_id))){
	$milestone_url_access[] = $this->session->userdata('user_id');
}
//check access for all user
 if(in_array($this->session->userdata('user_id'),$milestone_url_access)){?>
<a href="<?=site_url('user/tasks/task_detail').'/'.$task->id?>">
<?php echo $task->title;
if(in_array($this->session->userdata('user_id'),$task_follower_id)){
	echo '<small class="muted"> - Shared <small>';
}
?>
</a>
<?php }else {?>
<?=$task->title?>	
<?php }?>
</td>
<?php if(!$this->ion_auth->is_client()){?>
<td>
<?php echo $username = $this->common_model->get_username_id($task->user_id);?>	
</td>
<?php }?>
<td><?=$task->start_date?></td>
<td><?=$task->end_date?></td>
<td><?=$task->completed_percentage?> %</td>
<td><?=$task->time_spent?></td>
<td><?=$task->milestone_id?></td>
<td>
	<?php 
	// get fav
	$fav = $this->task_model->get_fav_tasks($task->id,$this->session->userdata('user_id'));	
	if(count($fav)==0){
	?>
	<a href="javascript:void(0)"  id="fav" onclick="add_fav_project(<?php echo $task->id;?>)"><i class="icon-star-empty" id="star"></i> </a>
	<?php }else{?>	
	<a href="javascript:void(0)"  id="nofav" onclick="remove_fav_project(<?php echo $task->id;?>)" ><i class="icon-star" id="star"></i> </a>
	<?php }?>
	</td>
</tr>
<?php $task_follower_id = array(); } else {
	$task_follower_id = array();
	foreach ($this->task_model->get_task_followers($task->id) as $key => $value){
		$task_follower_id[] = $value->user_id;
	}
	?>
<tr class="error tooltips" title="Closed Tasks">
<td>
<?php if($this->session->userdata('user_id') == $task->user_id  OR $this->ion_auth->is_admin($this->session->userdata('user_id')) OR $this->ion_auth->is_client($this->session->userdata('user_id')) OR in_array($this->session->userdata('user_id'),$task_follower_id) OR in_array($this->session->userdata('user_id'),$follower_id)){?>
<a href="<?=site_url('user/tasks/task_detail').'/'.$task->id?>">
<?php echo $task->title;

if(in_array($this->session->userdata('user_id'),$task_follower_id)){
	echo '<small class="muted"> - Shared <small>';
}

?>	
</a>
<?php }else{?>
<?=$task->title?>
<?php }?>
</td>
<?php if(!$this->ion_auth->is_client()){?>
<td>
<?php echo $username = $this->common_model->get_username_id($task->user_id);?>	
</td>
<?php }?>
<td><?=$task->start_date?></td>
<td><?=$task->end_date?></td>
<td><?=$task->completed_percentage?> %</td>
<td><?=$task->time_spent?></td>
<td><?=$task->milestone_id?></td>
<td>
	<?php 
	// get fav
	$fav = $this->task_model->get_fav_tasks($task->id,$this->session->userdata('user_id'));	
	if(count($fav)==0){
	?>
	<a href="javascript:void(0)"  id="fav" onclick="add_fav_project(<?php echo $task->id;?>)"><i class="icon-star-empty" id="star"></i> </a>
	<?php }else{?>	
	<a href="javascript:void(0)"  id="nofav" onclick="remove_fav_project(<?php echo $task->id;?>)" ><i class="icon-star" id="star"></i> </a>
	<?php }?>
	</td>
</tr>
<?php }
$task_follower_id = array();
 } ?>
</table>
<?php endif; }?>

<!-- Custom Tasks Group -->
<?php if( count($custom_taskgroups) > 0){?>
<h2>Custom Tasks</h2>
<?php }?>
<hr />
<!--Custom task group-->
<?php foreach($custom_taskgroups as $custom_taskgroup) { ?>
<?php 
$custom_tasks = $this->task_model->get_tasks_by_taskgroup_and_project($custom_taskgroup->id,$projects['id']);
if((count($custom_tasks))>0)
{	?>
<h4 class="widget-name"><i class="icon-folder-open"></i> <?= $custom_taskgroup->name?> 
  <?php if( $this->ion_auth->is_admin($this->session->userdata('user_id'))){?>  
 <a href="<?php echo site_url('user/tasks/get_custom_task_group/'.$custom_taskgroup->id);?>" class="edit_tg"> <i class="icon-edit tooltips" title="Edit"> </i></a

  <?php }?>  
  </h4>
<table width="100%" class="table table-condensed">
<thead>
<tr>
<th>Name</th>
<th>Assigned to</th>
<th>Start Date</th>
<th>Due date</th>
<th>% Completed</th>
<th>Time Spent</th>
<th>Milestone</th>
</tr>
</thead>
<?php 
$custom_task_follower_id = array();
foreach($custom_tasks as $custom_task) {	
	$custom_task_follower_id = array();
	foreach ($this->task_model->get_task_followers($custom_task->id) as $key => $value){
		$custom_task_follower_id [] = $value->user_id;
	}
	?>

<?php if(!$custom_task->is_completed) {?>
<tr 
<?php if($custom_task->completed_percentage == 100){ ?>
class="success"
<?php } elseif($custom_task->completed_percentage == 0) {?>
class="warning"
<?php }?>
>
<td>
<?php if($this->session->userdata('user_id') == $custom_task->user_id OR $this->ion_auth->is_admin($this->session->userdata('user_id')) OR $this->ion_auth->is_client($this->session->userdata('user_id')) OR in_array($this->session->userdata('user_id'),$custom_task_follower_id) OR in_array($this->session->userdata('user_id'),$follower_id)){?>
<a href="<?=site_url('user/tasks/task_detail').'/'.$custom_task->id?>">
<?php echo $custom_task->title;
if(in_array($this->session->userdata('user_id'),$custom_task_follower_id)){
	echo '<small class="muted"> - Shared <small>';
}
?>	
</a>
<?php }else {?>
<?=$custom_task->title?>	
<?php }?>
</td>
<td><?php
echo $username = $this->common_model->get_username_id($custom_task->user_id);
//echo $username['username'];
?></td>
<td><?=$custom_task->start_date?></td>
<td><?=$custom_task->end_date?></td>
<td><?=$custom_task->completed_percentage?> %</td>
<td><?=$custom_task->time_spent?></td>
<td><?=$custom_task->milestone_id?></td>
<td>
	<?php 
	// get fav
	$fav = $this->task_model->get_fav_tasks($custom_task->id,$this->session->userdata('user_id'));	
	if(count($fav)==0){
	?>
	<a href="javascript:void(0)"  id="fav" onclick="add_fav_project(<?php echo $custom_task->id;?>)"><i class="icon-star-empty" id="star"></i> </a>
	<?php }else{?>	
	<a href="javascript:void(0)"  id="nofav" onclick="remove_fav_project(<?php echo $custom_task->id;?>)" ><i class="icon-star" id="star"></i> </a>
	<?php }?>
	</td>

</tr>
<?php } else {
	$custom_closed_task_follower_id = array();
	foreach ($this->task_model->get_task_followers($custom_task->id) as $key => $value){
		$custom_closed_task_follower_id [] = $value->user_id;
	}
	?>
<tr class="error tooltips" title="Closed Tasks">
<td>
<?php if($this->session->userdata('user_id') == $custom_task->user_id OR $this->ion_auth->is_admin($this->session->userdata('user_id')) OR $this->ion_auth->is_client($this->session->userdata('user_id')) OR in_array($this->session->userdata('user_id'),$custom_task_follower_id) OR in_array($this->session->userdata('user_id'),$follower_id) ){?>
<a href="<?=site_url('user/tasks/task_detail').'/'.$custom_task->id?>">
<?php echo $custom_task->title;
if(in_array($this->session->userdata('user_id'),$task_follower_id)){
	echo '<small class="muted"> - Shared <small>';
}
?>	
</a>
<?php }else {?>
<?=$custom_task->title?>	
<?php }?>
</td>
<td><?php echo $username = $this->common_model->get_username_id($custom_task->user_id);?></td>
<td><?=$custom_task->start_date?></td>
<td><?=$custom_task->end_date?></td>
<td>

<?=$custom_task->completed_percentage?> %

</td>
<td><?=$custom_task->time_spent?></td>
<td><?=$custom_task->milestone_id?></td>
<td>
	<?php 
	// get fav
	$fav = $this->task_model->get_fav_tasks($custom_task->id,$this->session->userdata('user_id'));	
	if(count($fav)==0){
	?>
	<a href="javascript:void(0)"  id="fav" onclick="add_fav_project(<?php echo $custom_task->id;?>)"><i class="icon-star-empty" id="star"></i> </a>
	<?php }else{?>	
	<a href="javascript:void(0)"  id="nofav" onclick="remove_fav_project(<?php echo $custom_task->id;?>)" ><i class="icon-star" id="star"></i> </a>
	<?php }?>
	</td>
</tr>
<?php }$custom_task_follower_id=array();}?>
</table>
<?php }
$custom_task_follower_id = array();
 }?>


<!-- Add default groups-->
<div id="default_group" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
 <?= form_open('user/tasks/create_custom_taskgroup/'.$projects['id'])?>
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="myModalLabel">Add Custom Task Group</h3>
  </div>
  <div class="modal-body">
    <p>
	<strong>Name :</strong>
	<input type="text" class="span12" name="name"/>
	<strong>Description</strong> 
    <textarea placeholder="Add custom task group" class="span12" name="desc" id="desc"></textarea>    
    </p>
  
  </div>
  <div class="modal-footer">
    <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
    <button class="btn btn-info" type="submit">Save changes</button>
    </form>
  </div>
</div>

<script type="text/javascript">
<!--
	function add_fav_project(pId) {		
		$.post('<?= site_url("user/tasks/add_fav_tasks");?>',{id:pId}, function(data) {						
			location.reload();
	});
	}
	
	function remove_fav_project(pId) {	
		$.post('<?= site_url("user/tasks/remove_fav_tasks");?>',{id:pId}, function(data) {	
			location.reload();
		});
	}
	
	function create_task_group(){
		var project_id = <?php echo $projects['id'];?>;
		$.post('<?= site_url("user/tasks/remove_fav_tasks");?>',{id:pId}, function(data) {	
			location.reload();
		});
		
	}
	
$('.edit_tg').click(function(event) {		
  event.preventDefault();
  $.get(this.href, function(html) {
  $(html).appendTo('body').modal(); 
  });
});

function remove_follower(id,user_id){
	var removeid = '#follower'+id;
	var rname = $(removeid).attr('data-username');	
	 $.post('<?= site_url("user/tasks/remove_follower_project "); ?>',{id:id}, function(data) {			
			$(removeid).remove();
			/*$("#followers option[value='user_id']").remove();*/
			$("#followers").append('<option value="'+id+'">'+rname+'</option>');
			window.location.reload();
			});	
}
 

</script>
